import cv2

fourcc = cv2.VideoWriter_fourcc(*'MJPG')
vw = cv2.VideoWriter('../out.mp4', fourcc, 25, (1280, 720))

cv2.namedWindow('video', cv2.WINDOW_NORMAL)
cv2.resizeWindow('video', 640, 480)

cap = cv2.VideoCapture(0)

while cap.isOpened():
    ret, frame = cap.read()
    if ret:
        cv2.imshow('video', frame)
        vw.write(frame)
        key = cv2.waitKey(10)
        if key & 0xFF == ord('q'):
            break
    else:
        break

cap.release()
vw.release()
cv2.destroyAllWindows()
